package com.youlu.campus.admin.commission;

import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.entity.commission.dto.DistributorApplyQueryDTO;
import com.youlu.campus.entity.commission.dto.DistributorBuildLinkDTO;
import com.youlu.campus.entity.commission.dto.SharingCommissionDetailQueryDTO;
import com.youlu.campus.entity.commission.dto.UpdateStatusDistributorApplyDTO;
import com.youlu.campus.service.commission.DistributorApplyService;
import com.youlu.campus.service.commission.SharingCommissionDetailService;
import com.youlu.campus.service.platform.AppUserShareActivityService;
import com.yuelin.infrastructure.quantum.common.QResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
public class DistributorApplyController {

    @Autowired
    private DistributorApplyService distributorApplyService;

    @Autowired
    private SharingCommissionDetailService sharingCommissionDetailService;

    @Autowired
    private AppUserShareActivityService appUserShareActivityService;


    @PostMapping("/distributor/list")
    public QResult list (@RequestBody DistributorApplyQueryDTO dto){
        return QResult.success(distributorApplyService.list(dto));
    }

    @PostMapping("/distributor/update/status")
    public QResult updateStatus(@RequestBody @Validated UpdateStatusDistributorApplyDTO dto, BindingResult bindingResult){
        if (bindingResult.hasErrors()) {
            return QResult.fail(bindingResult.getFieldError().getDefaultMessage());
        }
        dto.setOptUserId(AuthUtils.getUserId());
        dto.setOptUserName(AuthUtils.getLoginName());
        distributorApplyService.updateStatus(dto);
        return QResult.success(null);
    }

    /**
     * 分销者分拥列表
     * @param dto
     * @return
     */
    @PostMapping("/distributor/commission/detail")
    public QResult list(@RequestBody SharingCommissionDetailQueryDTO dto){
        dto.setOptUserId(AuthUtils.getUserId());
        dto.setOptUserName(AuthUtils.getLoginName());
        return QResult.success(sharingCommissionDetailService.list(dto));
    }

    /**
     * 分销者分拥-导出
     * @param dto
     * @return
     */
    @PostMapping("/distributor/commission/detail/export")
    public QResult exportCommissionDetail(@RequestBody SharingCommissionDetailQueryDTO dto){
        dto.setOptUserId(AuthUtils.getUserId());
        dto.setOptUserName(AuthUtils.getLoginName());
        sharingCommissionDetailService.exportCommissionDetail(dto);
        return QResult.success(null);
    }

    /**
     * 分销者分享到第三方活动
     * @param userId
     * @return
     */
    @GetMapping("/distributor/share")
    public QResult shareActivity(@RequestParam String userId){
        return QResult.success(appUserShareActivityService.list(userId));
    }

    /**
     * 分销者分享复制链接
     * @param dto
     * @return
     */
    @PostMapping("/distributor/share/build-link")
    public QResult shareCopyLink(@RequestBody DistributorBuildLinkDTO dto){
        dto.setOptUserName(AuthUtils.getLoginName());
        return QResult.success(appUserShareActivityService.buildLink(dto));
    }

    @PostMapping("/distributor/delete/share-link")
    public QResult deleteShareLink(@RequestParam String id){
        appUserShareActivityService.delete(id);
        return QResult.success(null);
    }

}
